<template>
 <el-card>
      <div slot="header" class="clearfix">
        <my-breadcrumb>发布文章</my-breadcrumb>
      </div>
      <!-- 表格 -->
    <el-table
      v-loading="isLoading"
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="title"
        label="文章标题"
        width="180">
      </el-table-column>
      <el-table-column
        prop="total_comment_count"
        label="总评论数"
        width="180">
      </el-table-column>
      <el-table-column
        prop="fans_comment_count"
        label="粉丝评论数">
      </el-table-column>
      <el-table-column
        label="评论状态">
      <template slot-scope="scope">
        <el-tag v-if="scope.row.comment_status" type="success">正常</el-tag>
        <el-tag v-else type="danger">关闭</el-tag>
      </template>
      </el-table-column>
      <el-table-column
        label="操作"
        >
         <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.comment_status" @click="hSwitch(scope.row)" type="danger">关闭</el-button>
          <el-button size="mini" v-else type="success" @click="hSwitch(scope.row)">打开</el-button>
        </template>
      </el-table-column>
    </el-table>
      <!-- 分页 -->
    <el-pagination
      :disabled="isLoading"
      style="margin-top:20px;"
      background
      layout="prev, pager, next"
      :page-size="10"
      @current-change="hPageChange"
      :total="total_count">
    </el-pagination>
 </el-card>
</template>

<script>
export default {
  name: 'comment',
  data () {
    return {
      isLoading: false, // 加载中 loading
      total_count: 0, // 总条数
      page: 1, // 页码数
      // 评论数据
      tableData: [
      ]// 列表数据
    }
  },
  // 生命周期
  created () {
    this.loadComments()
  },
  methods: {
    // 获取参数
    buildParams () {
      const paramObj = {
        page: this.page,
        response_type: 'comment'
      }
      return paramObj
    },
    // 请求数据
    async loadComments () {
      const paramObj = this.buildParams()
      this.isLoading = true
      try {
        const res = await this.$axios({
          method: 'get',
          url: '/mp/v1_0/articles',
          params: paramObj
        })
        console.log(res)
        this.tableData = res.data.data.results
        this.total_count = res.data.data.total_count
        this.isLoading = false
      } catch (err) {
        console.log(err)
        this.$message.ereor('获取失败')
        this.isLoading = false
      }
    },
    // 分页
    hPageChange (curpage) {
      this.page = curpage
      this.loadComments()
    },
    // 打开或者关闭评论
    async hSwitch (row) {
      console.log(row)
      this.isLoading = true
      // 当前是否允许评论  打开是false  关闭是true
      console.log(row.comment_status)
      try {
        const res = await this.$axios({
          method: 'PUT',
          url: '/mp/v1_0/comments/status',
          // 传递数据
          // 文章id
          params: {
            article_id: row.id.toString()
          },
          data: {
            allow_comment: !row.comment_status
          }
        })
        console.log(res)
        this.isLoading = false
        this.$message.success('操作成功')
        // 本地更新数据  不需要重发请求浪费资源
        row.comment_status = !row.comment_status
      } catch (err) {
        console.log(err)
        this.isLoading = false
        this.$message.error('操作失败')
      }
    }
  }
}
</script>

<style>

</style>
